﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace PMSWenLvTing.Utility
{
    public class HttpHelper
    {
        /// <summary>
        /// 发送一个Post Http请求并返回请求结果
        /// </summary>
        /// <param name="Url">请求地址</param>
        /// <param name="postData">post请求参数，格式为verify=1and符号hotelid=1</param>
        /// <returns></returns>
        public static String SendHttpRequest(string Url, string postData)
        {
            Encoding encoding = Encoding.GetEncoding("UTF-8");


            Stream outstream = null;

            Stream instream = null;

            StreamReader sr = null;

            string url = Url;

            HttpWebRequest request = null;

            HttpWebResponse response = null;


            // 准备请求,设置参数

            request = WebRequest.Create(url) as HttpWebRequest;

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";

            string postDataStr = postData;


            byte[] data = encoding.GetBytes(postDataStr);

            request.ContentLength = data.Length;

            try
            {
                //如果地址不对获取请求数据流时也会报错
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Flush();
                outstream.Close();

                //如果地址不正确则会引发异常
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求                
                instream = response.GetResponseStream();
            }
            catch (Exception)
            {

                return "";
            }

            sr = new StreamReader(instream, encoding);

            //返回结果网页(html)代码

            string content = sr.ReadToEnd();
            return content;
        }

        /// <summary>
        /// 闪送发送Get请求
        /// </summary>
        /// <param name="Url">地址（含参数）</param>
        /// <returns>返回的值</returns>
        public static String SendHttpGetRequest(string Url)
        {
            string result = string.Empty;
            try
            {
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(Url);
                wbRequest.Method = "GET";
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    using (StreamReader sReader = new StreamReader(responseStream))
                    {
                        result = sReader.ReadToEnd();
                    }
                }
            }
            catch
            {

            }
            return result;
        }

        /// <summary>
        /// 腾讯发送Get请求
        /// </summary>
        /// <param name="Url">地址（含参数）</param>
        /// <returns>返回的值</returns>
        public static String SendHttpGetRequestHTTPS(string Url)
        {
            
            string result = string.Empty;
            try
            {
                HttpWebRequest wbRequest = null;
                #region 获取Http请求对象
                if (Url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    wbRequest = WebRequest.Create(Url) as HttpWebRequest;
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    wbRequest.ProtocolVersion = HttpVersion.Version11;
                    // 这里设置了协议类型。
                    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2; 
                    wbRequest.KeepAlive = false;
                    ServicePointManager.CheckCertificateRevocationList = true;
                    ServicePointManager.DefaultConnectionLimit = 100;
                    ServicePointManager.Expect100Continue = false;
                }
                else
                {
                    wbRequest = (HttpWebRequest)WebRequest.Create(Url);
                }
                #endregion

                wbRequest.Method = "GET";
                
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    using (StreamReader sReader = new StreamReader(responseStream))
                    {
                        result = sReader.ReadToEnd();
                    }
                }
            }
            catch
            {

            }
            return result;
        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            //总是接受  
            return true;
        }

        /// <summary>
        /// 扫呗支付接口的Http请求
        /// </summary>
        /// <param name="Url">请求地址</param>
        /// <param name="postData">post请求参数</param>
        /// <returns></returns>
        public static String SendJsonHttpRequest(string Url, string postData)
        {
            Encoding encoding = Encoding.GetEncoding("UTF-8");


            Stream outstream = null;

            Stream instream = null;

            StreamReader sr = null;

            string url = Url;

            HttpWebRequest request = null;

            HttpWebResponse response = null;


            // 准备请求,设置参数

            request = WebRequest.Create(url) as HttpWebRequest;

            request.Method = "POST";
            request.ContentType = "application/json";

            string postDataStr = postData;

            byte[] data = encoding.GetBytes(postDataStr);

            request.ContentLength = data.Length;
            try
            {
                //如果地址不对获取请求数据流时也会报错
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Flush();
                outstream.Close();

                //如果地址不正确则会引发异常
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求                
                instream = response.GetResponseStream();
            }
            catch (Exception)
            {

                return "";
            }

            sr = new StreamReader(instream, encoding);

            //返回结果网页(html)代码

            string content = sr.ReadToEnd();
            return content;
        }
    }
}
